require 'win32ole'
require 'pathname'

@@resultList = Array.new
@@sourceDir = Pathname.new(File.dirname(__FILE__)).realpath.to_s
@@foundLineDetail = Array.new
@@foundLineDetailSet = Array.new
class Result
        def initialize
                self.name = ''
                self.subFolderPath = ''
                self.lineNo = 0
                self.lineDetail = ''
        end
        attr_reader :name, :lineNo, :subFolderPath, :lineDetail
        attr_writer :name, :lineNo, :subFolderPath, :lineDetail
end

class LinesCounter
        def initialize(dir, fileFilter, lineFilter, fileType)
                raise "#{dir} is not folder" unless File.directory?(dir)
                @dir = dir
                @fileFilter, @lineFilter, @fileType = fileFilter, lineFilter, fileType
        end

        def count
                r = Result.new
                r.name = @dir
                sourceDir("", @dir, r)
                return r
        end
private
        def getPath(prefix, name)
                prefix=='' ? name : prefix + File::SEPARATOR + name
        end
        def sourceDir(prefix, dirName, collect)
                path = getPath(prefix, dirName)
                Dir.foreach(path) do
                        |x|
                        next if x =~/^(\.|\.\.)$/
                        next unless !x.index(".svn") && !x.index("prop-base") && !x.index("props") && !x.index("text-base") && !x.index("tmp")
                        r = Result.new
                        r.name = x
                        subpath = getPath(path, x)
                        if File.directory?(subpath)
                            sourceDir(path, x, r)
                        else
                            next unless @fileFilter.call(x)
                            countFile(path, x, r)
                            printResult(r)
                        end
                end
        end
        def countFile(prefix, fileName, collect)
                path = getPath(prefix, fileName)
                totalCount = 0
                lineNo = 0
                lineDetail = ''
                @@foundLineDetail = Array.new
				@@foundLineNo = Array.new
                File.foreach(path) do |x|
                	totalCount = totalCount + 1
                    #next unless @lineFilter.call(x)
                    #puts x
                    if x =~ /^\s*\!/
                        #commentCount = commentCount + 1
                    else
                    	#myarray = x.scan(/\#G\w+/)
                        #myarray.each do |str| 
                        #	if !@@foundLineDetailSet.include? str
                        #		lineDetail = Hash.new 
                        #		puts str
                        #		lineDetail['detail'] = str
                        #		lineDetail['no'] = totalCount
                        #		@@foundLineDetail.push lineDetail
                        #		@@foundLineDetailSet.push str
                        #	end
                        #end
                        lineDetail = Hash.new
                        if x=~ /^\s*MENU_FORM/
                        	lineDetail['detail'] = x
                            lineDetail['no'] = totalCount
                            lineDetail['type'] = 'MENU_FORM'
                        	@@foundLineDetail.push lineDetail
                        	@@foundLineDetailSet.push x
       					elsif x=~ /^\s*FORM\s+/
       						lineDetail['detail'] = x
                            lineDetail['no'] = totalCount
                            lineDetail['type'] = 'NORMAL_FORM'
                        	@@foundLineDetail.push lineDetail
                        	@@foundLineDetailSet.push x
                        elsif x=~ /^\s*QUERY_FORM\s+/
                        	lineDetail['detail'] = x
                            lineDetail['no'] = totalCount
                            lineDetail['type'] = 'QUERY_FORM'
                        	@@foundLineDetail.push lineDetail
                        	@@foundLineDetailSet.push x
                        elsif x=~ /^\s*REPORT_FORM\s+/
                        	lineDetail['detail'] = x
                            lineDetail['no'] = totalCount
                            lineDetail['type'] = 'REPORT_FORM'
                        	@@foundLineDetail.push lineDetail
                        	@@foundLineDetailSet.push x
                        elsif x=~ /^\s*TABLE_FORM\s+/
                        	lineDetail['detail'] = x
                            lineDetail['no'] = totalCount
                            lineDetail['type'] = 'TABLE_FORM'
                        	@@foundLineDetail.push lineDetail
                        	@@foundLineDetailSet.push x
                        end
                    end
                   
                end
                collect.subFolderPath = path.gsub(@@sourceDir,'').gsub(fileName,'').gsub('//','\\')
        end
end

def printResult(r)
    folderPath = ''
    resultObj = r
    modelName = ''
    if resultObj.subFolderPath == ''
        #folderPath = @@sourceDir
        #donothing
    else
        folderPath = resultObj.subFolderPath
        modelName = folderPath.scan(/\w+(?=[\/])/)[0]
    end
	@@foundLineDetail.each {|item| @@resultList.push "Name:#{resultObj.name},LineNo:#{item['no']},LineType:#{item['type']}\r"}
    #printStr = "Name:#{resultObj.name},LineNo:#{resultObj.lineNo},LineDetail:[#{resultObj.lineDetail}]"
    #@@resultList.push "#{printStr}\r"
    #puts "#{"-" * 80}"
end

def countFile()
    
    print "Pls input the src Folder first(if u want use defaultfolder,Pls push enter key!):"
    folderPath = $stdin.gets.chomp
    @@sourceDir = folderPath if folderPath != ''
    puts "Source Folder is [#{@@sourceDir}]."
    fileType = "dml"
    lc = LinesCounter.new(@@sourceDir,\
                          proc {|x| x =~ /^.*\.#{fileType}$/},            
                          proc {|x| x !~ /^\s*\s$/ },
                          fileType)
    lc.count
    writeResult
end

def writeResult()
    outputDir = Pathname.new(File.dirname(__FILE__)).realpath.to_s
    outputFileFullPath = outputDir + File::SEPARATOR + "result.txt"
    open("#{outputFileFullPath}",'w') {|f| f << @@resultList}
    puts "#{"-" * 100}"
    puts "Complete!The ResultList in #{outputFileFullPath} file,Pls Check It!"
end

countFile()

